package com.xiaozhameng.study.algorithm.string;

import java.util.Stack;

public class StringAdd {

    public static void main(String[] args) {
        String str1 = "99",
                str2 = "1";
        String solve = new StringAdd().solve(str1, str2);
        System.out.println(solve);
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 计算两个数之和
     *
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve(String s, String t) {
        // write code here
        char[] c1 = s.toCharArray();
        char[] c2 = t.toCharArray();
        Stack<Character> s1 = new Stack<>();
        Stack<Character> s2 = new Stack<>();
        Stack<Integer> res = new Stack<>();
        for (int i = 0; i < c1.length; i++) {
            s1.push(c1[i]);
        }
        for (int i = 0; i < c2.length; i++) {
            s2.push(c2[i]);
        }

        // 进位
        int p = 0;
        while (!s1.isEmpty() || !s2.isEmpty()) {
            int temp1 = s1.isEmpty() ? 0 : Integer.parseInt(s1.pop()+"");
            int temp2 = s2.isEmpty() ? 0 : Integer.parseInt(s2.pop()+"");
            int temp = temp1 + temp2 + p;
            if (temp < 10) {
                p = 0;
                res.push(temp);
            } else {
                p = temp / 10;
                res.push(temp % 10);
            }
        }
        if (p == 1){
            res.push(p);
        }

        StringBuilder builder = new StringBuilder();
        while (!res.isEmpty()) {
            builder.append(res.pop());
        }
        return builder.toString();
    }
}
